﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using com.huigo.Framework.ServiceUtils;

namespace com.huigo.Admin.Web.order
{
    public partial class QueryPolicyByInvoice : BasePage
    {
        private int pageIndex
        {
            get
            {
                return com.loto.Common.TypeParse.StrToInt(Request.QueryString["page"], 1);
            }
        }
        private DateTime queryStart
        {
            get
            {
                return com.loto.Common.Utils.IsDate(Request.QueryString["Start"]) ? Convert.ToDateTime(Request.QueryString["Start"]) : DateTime.Now.Date.AddMonths(-1);
            }
        }
        private DateTime queryEnd
        {
            get
            {
                return com.loto.Common.Utils.IsDate(Request.QueryString["End"]) ? Convert.ToDateTime(Request.QueryString["End"]) : DateTime.Now.Date;
            }
        }
        private long orderID
        {
            get
            {
                return com.loto.Common.TypeParse.StrToInt(Request.QueryString["orderID"], 0);
            }
        }
        private int InsType
        {
            get
            {
                return string.IsNullOrEmpty(Request.QueryString["InsType"]) ? int.MinValue : Convert.ToUInt16(com.loto.Common.Utils.SqlEscape(Request.QueryString["InsType"]));
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        protected void pageInfo_PageChanged(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Bind();
            }
        }
        private void Bind()
        {
            if (orderID > 0)
            {
                //if (queryStart.ToString("yyyy-MM-dd") != string.Empty && queryEnd.ToString("yyyy-MM-dd") != string.Empty)
                //{
                //    if (queryStart.ToString("yyyy-MM-dd") != "0001-01-01" && queryEnd.AddDays(1).ToString("yyyy-MM-dd") != "0001-01-02")
                //    {
                //        beginDate.Text = queryStart.ToString("yyyy-MM-dd");
                //        endDate.Text = queryEnd.AddDays(-1).ToString("yyyy-MM-dd");
                //    }
                //}
                beginDate.Text = queryStart.ToString("yyyy-MM-dd");
                endDate.Text = queryEnd.ToString("yyyy-MM-dd");
                long custFID = 0;
                string orderNo = string.Empty;
                List<RequestFuncParams> argList = new List<RequestFuncParams>();
                argList.Add(new RequestFuncParams("invoiceOrderID", orderID.ToString()));
                BaseService baseService = new BaseService();
                ParserXml parser = baseService.RequestEntryService(Dict.ERequestFunc.GET_INVOICE_ORDER_BY_ID, argList);
                if (parser != null && parser.RowsCount > 0)
                {
                    custFID = Convert.ToInt64(parser["OrderCustFid", 0].ToString());
                    orderNo = parser["OrderNo", 0].ToString();
                }
                StringBuilder sb = new StringBuilder();
                argList.Add(new RequestFuncParams("orderID", orderID.ToString()));
                argList.Add(new RequestFuncParams("custFID", custFID.ToString()));
                argList.Add(new RequestFuncParams("orderStart", queryStart.ToString("yyyy-MM-dd")));
                argList.Add(new RequestFuncParams("orderEnd", queryEnd.AddDays(1).ToString("yyyy-MM-dd")));
                argList.Add(new RequestFuncParams("pageSize", pageInfo.PageSize.ToString()));
                argList.Add(new RequestFuncParams("pageIndex", pageIndex.ToString()));
                argList.Add(new RequestFuncParams("insType", InsType.ToString()));

                string tbodyFormat = "<tr><td>{10}</td><td>{0}</td><td>{1}</td><td/>{2}</td><td>{3}</td><td>{4}</td><td>{5}</td><td>{6}</td><td>{7}</td><td>{8}</td><td>{9}</td></tr>";
                switch (InsType)
                {
                    case (int)Dict.InsType.Air:
                        sb.Append("<table><thead><tr><th>订单号</th><th>姓名</th><th>证件类型</th><th >证件号</th><th>出生日期</th><th>性别</th><th>航班号</th><th>保险起期</th><th>手机号码</th><th>电子邮箱</th><th>保单号</th></tr></thead><tbody>");
                   
                        parser = baseService.RequestEntryService(Dict.ERequestFunc.GET_INVOICE_ORDER_POLICY_AIR_CUST_NAME, argList);
                        if (parser != null && parser.RowsCount > 0)
                        {
                            pageInfo.RecordCount = Convert.ToInt16(parser["RecordCount", 0]);
                            pageInfo.CurrentPageIndex = pageIndex;
                            pageInfo.PageURLFormat = GetPagerUrl();
                            for (int i = 0; i < parser.RowsCount; i++)
                            {
                                sb.AppendFormat(tbodyFormat, parser["PolicyInsuredName", i].ToString(), parser["PolicyInsuredCredentialType", i].ToString(), parser["PolicyInsuredCredentialNo", i].ToString(), Convert.ToDateTime(parser["PolicyInsuredBirth", i].ToString().Replace("T", " ")).ToString("yyyy-MM-dd"), parser["PolicyInsuredGender", i].ToString(), parser["PolicyFlightNo", i].ToString(), Convert.ToDateTime(parser["PolicyFromPeriod", i].ToString().Replace("T", " ")).ToString("yyyy-MM-dd"), parser["PolicyInsuredTel", i].ToString(), parser["PolicyInsuredEmail", i].ToString(), parser["PolicyNo", i].ToString(), parser["OrderNo", i].ToString());

                            }
                            sb.Append("</tbody></table>");
                            LitInvoiceOrder.Text = sb.ToString();
                        }
                        break;
                    case (int)Dict.InsType.Travel:
                        sb.Append("<table><thead><tr><th>订单号</th><th>姓名</th><th>证件类型</th><th >证件号</th><th>出生日期</th><th>性别</th><th>航行线路</th><th style=\"width: 10%\">保险起期</th><th>手机号码</th><th>电子邮箱</th><th>保单号</th></tr></thead><tbody>");
                        parser = baseService.RequestEntryService(Dict.ERequestFunc.GET_INVOICE_ORDER_POLICY_TRAVEL_CUST_NAME, argList);
                        if (parser != null && parser.RowsCount > 0)
                        {
                            pageInfo.RecordCount = Convert.ToInt16(parser["RecordCount", 0]);
                            pageInfo.CurrentPageIndex = pageIndex;
                            pageInfo.PageURLFormat = GetPagerUrl();
                            for (int i = 0; i < parser.RowsCount; i++)
                            {
                                sb.AppendFormat(tbodyFormat, parser["PolicyInsuredName", i].ToString(), parser["PolicyInsuredCredentialType", i].ToString(), parser["PolicyInsuredCredentialNo", i].ToString(), Convert.ToDateTime(parser["PolicyInsuredBirth", i].ToString().Replace("T", " ")).ToString("yyyy-MM-dd"), parser["PolicyInsuredGender", i].ToString(), parser["PolicyTravelLine", i].ToString(),  Convert.ToDateTime(parser["PolicyFromPeriod", i].ToString().Replace("T", " ")).ToString("yyyy-MM-dd"), parser["PolicyInsuredTel", i].ToString(), parser["PolicyInsuredEmail", i].ToString(), parser["PolicyNo", i].ToString(), parser["OrderNo", i].ToString());
                            }
                            sb.Append("</tbody></table>");
                            LitInvoiceOrder.Text = sb.ToString();
                        }

                        break;
                }
            }
        }

        private string GetPagerUrl()
        {
            string url = "QueryPolicyByInvoice.aspx?";
            url += "page={0}";
            if (queryStart != DateTime.MinValue)
                url += "&Start=" + queryStart.ToString("yyyy-MM-dd");
            if (queryEnd != DateTime.MinValue)
                url += "&End=" + queryEnd.ToString("yyyy-MM-dd");
            if (orderID > 0)
                url += "&orderID=" + orderID;
            if (InsType > 0)
                url += "&InsType=" + InsType;
            return url;
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            string url = "QueryPolicyByInvoice.aspx?";
            url += "page=1";
            if (beginDate.Text != string.Empty)
                url += "&Start=" + beginDate.Text;
            if (endDate.Text != string.Empty)
                url += "&End=" + endDate.Text;
            if (orderID > 0)
                url += "&orderID=" + orderID;
            if (InsType > 0)
                url += "&InsType=" + InsType;
            Response.Redirect(url);
        }
    }
}
